package io.kiki.stack.netty.userProcessor;

import io.kiki.stack.netty.channelManager.ChannelContext;
import io.kiki.stack.netty.lifeCycle.AbstractLifeCycle;
import io.kiki.stack.netty.processor.BizContext;
import io.kiki.stack.netty.processor.BizContextImpl;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.Executor;

/**
 * Implements common function and provide default value.
 */

@Slf4j
@Setter
@Getter
public abstract class AbstractUserProcessor<T> extends AbstractLifeCycle implements UserProcessor<T> {

    /**
     * executor selector, default null unless provide one using its setter method
     */
    protected ExecutorSelector executorSelector;

    /**
     * Provide a default - {@link BizContextImpl} implementation of {@link BizContext}.
     */
    @Override
    public BizContext preHandleRequest(ChannelContext channelContext, T request) {
        return new BizContextImpl(channelContext);
    }

    /**
     * By default return null.
     */
    @Override
    public Executor getExecutor() {
        return null;
    }

    @Override
    public ClassLoader getBizClassLoader() {
        return null;
    }

    @Override
    public ExecutorSelector getExecutorSelector() {
        return this.executorSelector;
    }

    @Override
    public void setExecutorSelector(ExecutorSelector executorSelector) {
        this.executorSelector = executorSelector;
    }

    /**
     * By default, return false, means not deserialize and process biz logic in io thread
     */
    @Override
    public boolean processInIOThread() {
        return false;
    }

    /**
     * By default, return true, means discard requests which timeout already.
     */
    @Override
    public boolean timeoutDiscard() {
        return true;
    }
}
